package com.ruoyi.web.controller.shop.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.DO.member.MemberDO;
import com.ruoyi.common.entity.VO.member.*;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.web.controller.member.mapper.IBackMemberMapper;
import com.ruoyi.web.controller.member.mapper.IBackMemberVipTypeMapper;
import com.ruoyi.web.controller.shop.service.IBackShopMemberVipBenefitService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 会员权益管理
 */
@Controller
@RequestMapping("/back/shopMemberVipBenefit")
public class BackShopMemberVipBenefitController extends BaseController {
    private String prefix = "shop/member/vipBenefit";

    @Resource
    private IBackShopMemberVipBenefitService vipBenefitService;

    @Resource
    private IBackMemberVipTypeMapper memberVipTypeMapper;

    @Resource
    private IBackMemberMapper memberMapper;

    /**
     * 页面-加油权益列表
     *
     * @return
     */
    @RequiresPermissions("back:shopMemberVipBenefit:petrolList")
    @GetMapping("/petrolList")
    public String petrolList(ModelMap mmap) {
        if (0 == ShiroUtils.getShopId()) {
            return "redirect:/back/shop/switchShop";
        }
        MemberVipTypeSearchVO searchVO = new MemberVipTypeSearchVO();
        searchVO.setIsDelete(0);
        searchVO.setIsEnable(1);
        List<MemberVipTypeVO> vipTypeList = memberVipTypeMapper.searchList(searchVO);
        mmap.put("vipTypeList", vipTypeList);
        return prefix + "/petrolList";
    }

    /**
     * 页面-充值权益列表
     *
     * @return
     */
    @RequiresPermissions("back:shopMemberVipBenefit:rechargeList")
    @GetMapping("/rechargeList")
    public String rechargeList(ModelMap mmap) {
        if (0 == ShiroUtils.getShopId()) {
            return "redirect:/back/shop/switchShop";
        }
        MemberVipTypeSearchVO searchVO = new MemberVipTypeSearchVO();
        searchVO.setIsDelete(0);
        searchVO.setIsEnable(1);
        List<MemberVipTypeVO> vipTypeList = memberVipTypeMapper.searchList(searchVO);
        mmap.put("vipTypeList", vipTypeList);
        return prefix + "/rechargeList";
    }


    /**
     * 会员独享折扣-页面跳转
     *
     * @return
     */
    @RequiresPermissions("back:shopMemberVipBenefit:customList")
    @GetMapping("/customList/{memberId}")
    public String customList(@PathVariable("memberId") Long memberId, ModelMap mmap) {
        if (0 == ShiroUtils.getShopId()) {
            return "redirect:/back/shop/switchShop";
        }
        mmap.put("memberId", memberId);
        mmap.put("memberPhone", memberMapper.selectByPrimaryKey(memberId).getPhone());
        return prefix + "/petrolExtList";
    }


    /**
     * 数据查询-加油权益列表，充值权益列表
     */
    @RequiresPermissions(value = {"back:shopMemberVipBenefit:rechargeList", "back:shopMemberVipBenefit:petrolList"}, logical = Logical.OR)
    @PostMapping("/searchList")
    @ResponseBody
    public TableDataInfo searchList(MemberVipBenefitSearchVO searchVO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        searchVO.setShopId(ShiroUtils.getShopId());
        startPage();
        List<MemberVipBenefitVO> list = vipBenefitService.searchList(searchVO);
        return getDataTable(list);
    }

    /**
     * 数据查询-独享权益列表
     */
    @RequiresPermissions("back:shopMemberVipBenefit:customList")
    @PostMapping("/searchCustomList")
    @ResponseBody
    public TableDataInfo searchCustomList(MemberVipBenefitSearchVO searchVO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        searchVO.setShopId(ShiroUtils.getShopId());
        startPage();
        //拿出当前等级的设置
        MemberDO memberInfo = memberMapper.selectByPrimaryKey(searchVO.getMemberId());
        searchVO.setMemberVipTypeId(memberInfo.getVipTypeId());
        List<MemberVipBenefitExtVO> list = vipBenefitService.searchCustomList(searchVO);
        for (MemberVipBenefitExtVO one:list
             ) {
            if(null == one.getIsEnable()){
                one.setIsEnable(0);
            }
        }
        return getDataTable(list);
    }

    /**
     * 数据编辑-加油和充值权益
     */
    @Log(title = "站点-会员权益管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions(value = {"back:shopMemberVipBenefit:rechargeEdit", "back:shopMemberVipBenefit:petrolEdit"}, logical = Logical.OR)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(MemberVipBenefitVO editVO) {
        editVO.setShopId(ShiroUtils.getShopId());
        editVO.setUpdateBy(ShiroUtils.getSysUser().getUserName());
        try {
            vipBenefitService.edit(editVO);
            return success();
        } catch (BusinessException e) {
            return error(e.getMessage());
        }
    }

    /**
     * 数据编辑-会员独享权益配置
     */
    @Log(title = "站点-会员独享权益管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("back:shopMemberVipBenefit:customEdit")
    @PostMapping("/customEdit")
    @ResponseBody
    public AjaxResult customEdit(MemberVipBenefitExtVO editVO) {
        editVO.setShopId(ShiroUtils.getShopId());
        editVO.setUpdateBy(ShiroUtils.getSysUser().getUserName());
        try {
            vipBenefitService.editExt(editVO);
            return success();
        } catch (BusinessException e) {
            return error(e.getMessage());
        }
    }

}
